console.log("aa");


/**
 * login for get phone code and save it in the cookie
 */

var phone;
var code;

function getCode(){
    var phone = document.getElementById('phone').value;
    var url = 'http://api.wefi.com.cn/apiv2/?r=api/sendsms';

    if( phone != '' ){
        $.ajax({
            method: 'POST',
            dataType: 'text',
            url: url,
            data: 'phone=' + phone,
            success: function(msg){
                console.log(msg)
                msg = JSON.parse(msg);
                code = msg.resultinfo;
                setCookie('logininfo', phone)
            }
        });
    }
    else{
        alert('手机号码不正确')
    }
}

function Enter4K(){
    var phone = document.getElementById('phone').value;
    var codeInput = document.getElementById('code').value;

    if( phone != '' ){
        if(codeInput == code){
            window.location.href = '../../index.html'
        }
        else{
            alert('验证码不正确')
        }
    }
    else{
        alert('请输入手机号码')
    }

}


/**
 * 判断设备是否绑定,是否在线
 */


function init(){
    var url = location.href;
    var position = url.lastIndexOf('=');
    var open_id = url.substring(position+1, url.length);

    if( !open_id ){
        alert('请先绑定openid')
    }
    else{
        var wx_url = config.wx_info.replace('%s', open_id);
        proxy(wx_url, function(res){
            var response = JSON.parse(res);
            if( response.row ){
                setCookie('wxinfo', res);

                ActiveStatus(function(response){
                    if( ! response.status.errorcode == '0k' ){
                        alert('设备不在线')
                    }
                });
            }
            console.log(response)
        });
    }
}














function getDownload() {
    var wxinfo = getCookie('wxinfo');
    var wx_info = JSON.parse(wxinfo).row;
    var IP = wx_info.ctrlServer;
    var PORT = wx_info.ctrlPort;
    var TOKEN = wx_info.token;
    var down_url = config.all_download_status.replace('%s', IP).replace('%s', PORT).replace('%s', TOKEN);

    proxy(down_url, function (res) {
        var response = JSON.parse(res);
        var list = response.data.list;
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var listDown = list[i]

            html += getDownlist(listDown);

        }

        document.getElementById('downloadList').innerHTML = html;


        //console.log(list)
    });
}

function getDownlist(listDown) {
    console.log(listDown);
    try{
        var progress = listDown.progress;
        var speed = Math.round(listDown.speed/1024/1024);

        var complete = Math.ceil((progress.complete/progress.total)*100);
        var completeSize =Math.round(((progress.complete/progress.total)*listDown.filesize)/1024/1024);

    }catch (e){
        var speed = complete = completeSize =0;
    }
    var total = Math.round(listDown.filesize/1024/1024);

    return html = '<div class="row">'
        + '<div class="col-lg-4 col-sm-4 col-xs-4 video-nopadr">'
        + '<div class="full-content relative video-d-img">'
        + '<img src="' + listDown.pic + '"/>'
        + '</div>'
        + '</div>'

        + '<div class="col-lg-8 col-sm-8 col-xs-8">'
        + '<div class="full-content relative video-d-info">'
        + '<h5 class="video-d-name">' + listDown.videoname + '</h5>'
        + '<ul>'
        + '<li>'+complete+'%</li>'
        + '<li>'+completeSize+'MB / '+total+'MB<span> '+speed+'m/s</span></li>'
        + '</ul>'
        + '<div></div>'
        + '</div>'
        + '</div>'
        + '</div>'

        + '<div class="row pd5">'
        + '<div class="col-lg-12 col-sm-12 col-xs-12">'
        + '<div class="full-content bottom-bottom1"></div>'
        + '</div>'
        + '</div>';
}